package com.abel.quiz.interceptors;

import com.abel.quiz.common.CookieUtil;
import com.abel.quiz.common.DateUtil;
import com.abel.quiz.common.vo.AjaxResp;
import com.abel.quiz.common.vo.WechatResp;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 微信小程序登录拦截器
 * Created by abel.lin on 2017/2/8.
 */
@Component
public class WechatInterceptor extends HandlerInterceptorAdapter {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException, IOException {

        String token = request.getParameter("token");
        /*WechatResp wechatResp = CookieUtil.get(request,"wx_token",WechatResp.class);
        boolean isAuth = false;
        if(wechatResp != null && StringUtils.isNotBlank(wechatResp.getToken())){
            Date expireTime = wechatResp.getExpireTime();
            String authToken = wechatResp.getToken();
            if(new Date().getTime() < expireTime.getTime() && token.equals(authToken)){
                isAuth = true;
            }
        }else if(StringUtils.isBlank(token)){
            isAuth = true;
        }
        if(!isAuth){
            WechatResp ajax = new WechatResp(AjaxResp.CodeArgs.NO);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(ajax.toJson());

            return false;
        }*/

        return true;
    }


}